package cn.msqweb.controller;

import cn.msqweb.entity.SysRole;
import cn.msqweb.model.Result;
import cn.msqweb.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "系统角色接口管理")
@RequestMapping("sys/role")
@RestController
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;


    @ApiOperation("查询系统所有角色列表")
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<List<SysRole>> loadSysRoleList() {
        List<SysRole> list = sysRoleService.querySysRoleList();
        return Result.success(list);
    }



    @ApiOperation("查询系统角色列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:role:list')")
    public Result<Page<SysRole>> loadSysRoleList1(@ApiParam("当前页") @RequestParam Long current,
                                                  @ApiParam("总条数") @RequestParam Long size,
                                                  @ApiParam("查询条件") @RequestParam(required = false) String roleName) {
        Page<SysRole> sysRolePage = new Page<>();
        Page<SysRole> page = sysRoleService.page(sysRolePage, new LambdaQueryWrapper<SysRole>()
                .like(StringUtils.hasText(roleName), SysRole::getRoleName, roleName)
                .orderByDesc(SysRole::getCreateTime));
        return Result.success(page);
    }

    @ApiOperation("新增角色")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:role:save')")
    public Result<String> saveSysRole(@ApiParam("权限集合对象")@RequestBody SysRole sysRole){
        boolean save =  sysRoleService.saveSysRole(sysRole);
        return Result.handler(save);
    }
    @ApiOperation("根据标识查询角色详情")
    @GetMapping("info/{roleId}")
    @PreAuthorize("hasAuthority('sys:role:info')")
    public Result<SysRole> loadSysRoleDetail(@ApiParam("角色标识") @PathVariable Long roleId) {
        SysRole sysRole = sysRoleService.querySysRoleDetailById(roleId);
        return Result.success(sysRole);
    }
    @ApiOperation("修改系统角色")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:role:update')")
    private Result<String> modifySysRole(@ApiParam("角色对象") @RequestBody SysRole sysRole){
        Boolean modified = sysRoleService.modifySysRole(sysRole);
        return Result.handler(modified);
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping
    @PreAuthorize("hasAuthority('sys:role:delete')")
    public Result<String> removeSysRole(@ApiParam("角色id集合") @RequestBody List<Long> roleIds) {
        Boolean removed = sysRoleService.removeSysRoleByIds(roleIds);
        return Result.handler(removed);
    }
}